package com.cfun.mylearn.Support.api;

import android.text.TextUtils;

import com.cfun.mylearn.Support.utils.CommonUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by flyop on 19/06/2016.
 */
public class APIParams
{
    public static final String DAGUAN_APP_ID = "4ff4fd5cc08eb08fe5d8d74f";
    public static final String DAGUAN_APP_SECRET = "rBsUk6UhcwDQpp6btX9rVytG";
    public static final String DAGUAN_SIGNATURE_METHOD = "sha256";

    public static final String APP_VERSION = "appversion";
    public static final String APP_VERSION_CODE = "appversioncode";
    public static final String API_VERSION_KEY = "api_version";
    public static final String APP_VERSION_KEY = "app_version";
    public static final String MARKET_CHANNEL_KEY = "market-channel";
    // public static final String API_VERSION_VALUE = "13";
    // public static final String APP_VERSION_VALUE = "a-V1.1.0";

    public static final String SESSION_ID = "session_id";
    public static final String ACCOUNT_ID = "account_id";
    public static final String DEVICE_ID = "device_id";
    public static final String SIGNATURE_METHOD = "signature_method";
    public static final String SIGNATURE = "signature";
    public static final String TIMESTAMP = "timestamp";
    public static final String APP_ID = "app_id";
    public static final String RANDOM_TIME = "RANDOM_TIME";
    public static final String OS_TYPE = "os_type";
    public static final String DEVICE_TYPE = "device_type";
    public static final String DEVICE_UUID = "device_uuid";
    public static final String ANDROID_OS_TYPE = "20";
    public static final String ANDROID_DEVICE_TYPE = "10";

    private Map<String, String> params = new HashMap<>();// 参数
    private Map<String, String> headers = new HashMap<>();// headers

    private final String url;

    public String getUrl() {
        return url;
    }

    public APIParams(final String url, boolean zhejiangDaily) {
        this(url);

        if(!zhejiangDaily) {
            headers.remove(APP_VERSION);
            headers.remove(APP_VERSION_CODE);
            headers.remove("User-agent");
            String defaultAgent = System.getProperty("http.agent");
            if (!TextUtils.isEmpty(defaultAgent)) {
                headers.put("User-agent", defaultAgent);
            } else {
                headers.put("User-agent", "()");
            }
        }
    }

    public APIParams(final String url) {
        this.url = url;
        headers.put(APP_VERSION, CommonUtils.getVersionName());
        headers.put(APP_VERSION_CODE, CommonUtils.getVersionCode());
        String defaultAgent = System.getProperty("http.agent");

        if (StringUtils.isNotEmpty(defaultAgent)) {
            headers.put("User-agent", defaultAgent + CommonUtils.getCustomAgent());
        } else {
            headers.put("User-agent", "()" + CommonUtils.getCustomAgent());
        }
        headers.put("Accept-Encoding", "gzip");
        // headers.put(API_VERSION_KEY, API_VERSION_VALUE);
        // headers.put(MARKET_CHANNEL_KEY,
        // ResourceReader.readMetaString(MARKET_CHANNEL_KEY));
    }

    /**
     * 适合于构造GET请求的URL地址
     *
     * @return
     */
    public String getUrlWithParams() {
        StringBuilder sb = new StringBuilder(url);
        if (!params.isEmpty()) {
            sb.append("?");
            for (String key : params.keySet()) {
                String value = params.get(key);
                if (value != null) {
                    sb.append(key).append("=").append(params.get(key)).append("&");
                }
            }
        }

        if (APIAgent.apiConfig != null) {
            Object key = url.replace(APIAgent.URL_SERVER_HOST, "");
            if (APIAgent.apiConfig.containsKey(key)) {
                Object extra_value = APIAgent.apiConfig.get(key);
                sb.append(params.isEmpty() ? "?" : "").append(extra_value);
            }
        }

        return sb.toString();
    }

    public void setParams(String key, String value) {
        this.params.put(key, value);
    }

    public void setTimestamp(String timestamp) {
        setParams(TIMESTAMP, timestamp);
    }

    public void setSignatureMethod() {
        setParams(SIGNATURE_METHOD, DAGUAN_SIGNATURE_METHOD);
    }

    public void setAppId() {
        setParams(APP_ID, DAGUAN_APP_ID);
    }

    // public void setAppVersion() {
    // setParams(APP_VERSION_KEY, ResourceReader.readAppVersion());
    // }
    //
    // public void setApiVersion() {
    // setParams(API_VERSION_KEY, API_VERSION_VALUE);
    // }

    public void setSignature(String timestamp) {
        StringBuilder sb = new StringBuilder();
        sb.append(APP_ID).append("=").append(DAGUAN_APP_ID);
        sb.append(DEVICE_TYPE).append("=").append(Build.MODEL);
        sb.append(OS_TYPE).append("=").append("Android ").append(Build.VERSION.RELEASE);
        String device_uuid = ResourceReader.getUuid();
        // String device_uuid = ResourceReader.readUuid();
        sb.append(DEVICE_UUID).append("=").append(device_uuid);
        sb.append(SIGNATURE_METHOD).append("=").append(DAGUAN_SIGNATURE_METHOD);
        sb.append(TIMESTAMP).append("=").append(timestamp);
        sb.append(DAGUAN_APP_SECRET);
        String signature = EncodeUtils.encodeSha256Hex(sb.toString());
        setParams(SIGNATURE, signature);
    }

    public boolean isEmpty() {
        return params.isEmpty();
    }

    public Map<String, String> getParams() {
        return params;
    }

    public Map<String, String> getCommonHeaders() {
        return headers;
    }
}
